package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Account;

public class AccountDAOImpl extends BaseHibernateDAOImpl<Account> implements AccountDAO {
	public Account getAccountByAccountID(String accountID) {
		Account account;
		try {
			account = (Account) getSession().get(Account.class, accountID);
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return account;
	}

	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByCondition(String hql) {
		List<Account> accounts;
		try {
			Query query = getSession().createQuery("from Account" + hql);
			accounts = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return accounts;
	}

	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByConditionByPage(String hql, int currentPage, int lineSize) {
		List<Account> accounts;
		try {
			Query query = getSession().createQuery("from Account" + hql);
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			accounts = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return accounts;
	}

	public String getMaxAccountCodeFromAccount() {
		String accountCode;
		try {
			Query query = getSession().getNamedQuery("MaxAccountCodeFromAccountQuery");
			accountCode = (String) query.uniqueResult();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return accountCode;
	}
	
	public Account getAccountByAccountCode(String accountCode) {
		Account account;
		try {
			Query query = getSession().getNamedQuery("getAccountByAccountCodeQuery");
			query.setString("accountCode", accountCode);
			account = (Account) query.uniqueResult();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return account;
	}

}
